package org.ruoyi.interview.controller;

import cn.hutool.json.JSONArray;
import lombok.RequiredArgsConstructor;
import org.ruoyi.common.core.utils.StringUtils;
import org.ruoyi.interview.domain.vo.InterviewQuestionCategoryWithCountVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.common.core.validate.AddGroup;
import org.ruoyi.common.core.validate.EditGroup;
import org.ruoyi.common.web.core.BaseController;
import org.ruoyi.common.log.annotation.Log;
import org.ruoyi.common.log.enums.BusinessType;
import org.ruoyi.common.mybatis.core.page.PageQuery;
import org.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.ruoyi.interview.domain.bo.InterviewQuestionCategoryBo;
import org.ruoyi.interview.domain.vo.InterviewQuestionCategoryVo;
import org.ruoyi.interview.service.IInterviewQuestionCategoryService;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 面试题分类控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/interview/category")
public class InterviewQuestionCategoryController extends BaseController {

    private final IInterviewQuestionCategoryService interviewQuestionCategoryService;

    /**
     * 查询面试题分类列表
     */
    @GetMapping("/list")
    public TableDataInfo<InterviewQuestionCategoryVo> list(InterviewQuestionCategoryBo bo, PageQuery pageQuery) {
        return interviewQuestionCategoryService.selectInterviewQuestionCategoryList(bo, pageQuery);
    }


    /**
     * 查询面试题分类列表
     */
    @GetMapping("/listWithCount")
    public TableDataInfo<InterviewQuestionCategoryWithCountVo> listWithCount(InterviewQuestionCategoryBo bo, PageQuery pageQuery) {
        return interviewQuestionCategoryService.selectInterviewQuestionCategoryListWithCount(bo, pageQuery);
    }


    /**
     * 查询所有面试题分类列表（用于前端级联选择）
     */
    @GetMapping("/listAll")
    public R<List<InterviewQuestionCategoryVo>> listAll(InterviewQuestionCategoryBo bo) {
        List<InterviewQuestionCategoryVo> list = interviewQuestionCategoryService.selectInterviewQuestionCategoryList(bo);
        return R.ok(list);
    }

    /**
     * 获取面试题分类详细信息
     *
     * @param categoryId 分类主键
     */
    @GetMapping("/info{categoryId}")
    public R<InterviewQuestionCategoryVo> getInfo(@PathVariable("categoryId") Long categoryId) {
        return R.ok(interviewQuestionCategoryService.selectInterviewQuestionCategoryById(categoryId));
    }

    /**
     * 新增面试题分类
     */
    @Log(title = "面试题分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody InterviewQuestionCategoryBo bo) {
        return toAjax(interviewQuestionCategoryService.insertInterviewQuestionCategory(bo));
    }

    /**
     * 批量更新分类
     */
    @Log(title = "批量更新分类", businessType = BusinessType.INSERT)
    @PutMapping("/updateOrder")
    public R<Void> updateOrder(@RequestBody Collection<InterviewQuestionCategoryBo>  bo) {
        return toAjax(interviewQuestionCategoryService.updateOrderBatch(bo));
    }

    /**
     * 修改面试题分类
     */
    @Log(title = "面试题分类", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody InterviewQuestionCategoryBo bo) {
        return toAjax(interviewQuestionCategoryService.updateInterviewQuestionCategory(bo));
    }



    /**
     * 修改面试题分类状态
     */
    @Log(title = "面试题分类", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody InterviewQuestionCategoryBo bo) {
        return toAjax(interviewQuestionCategoryService.updateCategoryStatus(bo.getCategoryId(), bo.getStatus()));
    }

    /**
     * 删除面试题分类
     *
     * @param categoryIds 分类主键串
     */
    @Log(title = "面试题分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete{categoryIds}")
    public R<Void> remove(@PathVariable Long[] categoryIds) {
        return toAjax(interviewQuestionCategoryService.deleteInterviewQuestionCategoryByIds(Arrays.asList(categoryIds)));
    }

    /**
     * 通过ai获取更多分类
     * @param map 分类主键串
     */
    @PostMapping("/getCategoryByAi")
    public R<List<String>> getCategoryByAi(@RequestBody Map map ) {
        Long id = Long.parseLong(map.get("parentId").toString());
        String prompt = (String) map.get("prompt");
        try {
            List<String> categoryByAi = interviewQuestionCategoryService.getCategoryByAi(id, prompt);
            return R.ok(categoryByAi);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
